{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 概要\n",
    "\n",
    "* ruby-ldapの使い方について\n",
    "* jorurimailで使っているものを採用\n",
    "    * ruby-ldap (0.9.19)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# インストール\n",
    "\n",
    "* Ubuntu\n",
    "    * `$ sudo apt-get install libldap2-dev libsasl2-dev`\n",
    "    * 以下は不要かもしれない\n",
    "        * `$ sudo apt-get install ldap-utils slapd`\n",
    "\n",
    "* Gemfile\n",
    "\n",
    "```\n",
    "================================================\n",
    "# frozen_string_literal: true\n",
    "source \"https://rubygems.org\"\n",
    "\n",
    "# gem \"rails\"\n",
    "\n",
    "gem 'ruby-ldap', '~> 0.9.11'\n",
    "================================================\n",
    "```\n",
    "\n",
    "* bundler\n",
    "    * `$ bundle install --path vendor/bundle`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 環境の確認"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "### 階層や変数の確認\n",
    "#Dir.pwd\n",
    "#p $:\n",
    "\n",
    "### 使えるクラス一覧を出力\n",
    "#require '/root/git_jupyter_notebook/Ruby/vendor/bundle/ruby/2.3.0/gems/ruby-ldap-0.9.19/lib/ldap'\n",
    "#ObjectSpace.each_object(Class) do |klass|\n",
    "#  puts klass.name\n",
    "#end"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# LDAP接続"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "#<LDAP::Conn:0x007f90640b3668 @args=[\"10.136.3.181\", 3390], @sasl_quiet=false>\n"
     ]
    }
   ],
   "source": [
    "#require '/root/git_jupyter_notebook/Ruby/vendor/bundle/ruby/2.3.0/gems/ruby-ldap-0.9.19/lib/ldap'\n",
    "require '/Users/ftakao2007/work/jupyter/jupyter_notebook/Ruby/vendor/bundle/ruby/2.3.0/gems/ruby-ldap-0.9.19/lib/ldap'\n",
    "\n",
    "### LDAPサーバーに接続\n",
    "conn = LDAP::Conn.new('10.136.3.181', 3390)\n",
    "conn.set_option(LDAP::LDAP_OPT_PROTOCOL_VERSION, 3)\n",
    "conn.bind(\"cn=Manager,dc=joruri,dc=example,dc=com\",\"pass\")\n",
    "p conn\n",
    "conn.unbind"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# LDAP検索"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[\"joruri_test@joruri.example.com\"]\n",
      "[\"0050011\"]\n",
      "\"\\xE4\\xB8\\x80\\xE8\\x88\\xAC\\xE7\\xA4\\xBE\\xE5\\x93\\xA1\"\n",
      "一般社員\n"
     ]
    }
   ],
   "source": [
    "#require '/root/git_jupyter_notebook/Ruby/vendor/bundle/ruby/2.3.0/gems/ruby-ldap-0.9.19/lib/ldap'\n",
    "require '/Users/ftakao2007/work/jupyter/jupyter_notebook/Ruby/vendor/bundle/ruby/2.3.0/gems/ruby-ldap-0.9.19/lib/ldap'\n",
    "\n",
    "### LDAPサーバーに接続\n",
    "conn = LDAP::Conn.new('10.136.3.181', 3390)\n",
    "conn.set_option(LDAP::LDAP_OPT_PROTOCOL_VERSION, 3)\n",
    "conn.bind(\"cn=Manager,dc=joruri,dc=example,dc=com\",\"pass\")\n",
    "\n",
    "### LDAP検索\n",
    "base='dc=joruri,dc=example,dc=com'\n",
    "scope = LDAP::LDAP_SCOPE_SUBTREE\n",
    "filter = '(uid=0050011)'\n",
    "### 取得する属性を限定する場合\n",
    "attrs = ['mail', 'uid','title']\n",
    "### 属性限定なし\n",
    "#attrs = []\n",
    "\n",
    "conn.search(base, scope, filter, attrs) do |e|\n",
    "  #p e\n",
    "  p e[\"mail\"]\n",
    "  p e[\"uid\"]\n",
    "  p e[\"title\"] [0]\n",
    "  # utf-8にエンコーディングしてputsで出力すると化けない\n",
    "  puts e[\"title\"] [0].force_encoding(\"utf-8\")\n",
    "end\n",
    "\n",
    "conn.unbind"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 参考\n",
    "\n",
    "* [Ruby で扱う LDAP のススメ](https://www.slideshare.net/tasheeen/ruby-ldap-5107901)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Ruby 2.3.1",
   "language": "ruby",
   "name": "ruby"
  },
  "language_info": {
   "file_extension": ".rb",
   "mimetype": "application/x-ruby",
   "name": "ruby",
   "version": "2.3.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
